/**	
 * Author : Mht
 * Desc : For 2016 RDM 
 */
package dao;

import java.io.Serializable;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import dynamic.RoutingContextHolder;

/**
 * @author Mht
 * @date Jun 14, 2016
 */
//@Component
//@Transactional(propagation = Propagation.REQUIRED)
public class GenericHibernateBaseDao<T extends Serializable, PK extends Serializable> extends HibernateDaoSupport {

//	@Resource
//	@Qualifier("transactionManager")
//	public HibernateTransactionManager transactionManager;

	@Resource
//	@Qualifier("sessionFactory")
	public void setSuperSessionFactory(SessionFactory sessionFactory) {

		super.setSessionFactory(sessionFactory);
	}

	@Resource
//	@Qualifier("sessionFactory")
	public HibernateTemplate createHibernateTemplate(SessionFactory sessionFactory) {
		return new HibernateTemplate(sessionFactory);
	}
	
	protected Class<T> persistentClass;

	public GenericHibernateBaseDao() {

	}

	public GenericHibernateBaseDao(final Class<T> persistentClass) {
		this.persistentClass = persistentClass;
	}

	public void clearTable() {

		currentSession().createQuery("delete " + persistentClass.getSimpleName()).executeUpdate();
	}

	public void save(T object) {
		currentSession().save(object);
		currentSession().flush();
	}

	public T saveRemote(T object) {
		T obj = getHibernateTemplate().merge(object);
		getHibernateTemplate().flush();
		return obj;
	}
}
